<?php

/*
 * Controller/TemplateController.php
 */

namespace RHX\Controller;

class TemplateController extends RHXController {

    public function indexAction() {
	$list		 = $this->getService('Table\PlanningTemplateTable')
		->getAll();
	$codeList	 = $this->getService('Table\CodeActiviteTable')
		->getAll();
	// Parcours de la liste des Codes
	$codes		 = array();
	foreach ($codeList as $code) {
	    $codes[$code->id] = $code;
	}

	return array(
	    'templates'	 => $list,
	    'codes'		 => $codes,
	);
    }

    public function editAction() {
	// Changment de layout
	$this->layout('layout/ajax');

	// Données en Post
	$post		 = $this->getRequest()->getPost();
	$site_id	 = (int) $post->get('site_id');
	$num		 = (int) $this->params()->fromRoute('id', 0);
	$nohead		 = $post->get('nohead');
	$fullscreen	 = $post->get('fullscreen');

	// Listes
	$templates	 = $this->getService('Table\PlanningTemplateTable')
		->getTemplatesBySite($site_id, $num);
	$template	 = $this->getService('Table\PlanningTemplateTable')
		->getOne($num, $site_id);
	$empl		 = $this->getService('Table\EmployeTable')
		->getEmployesBySite($site_id);
	$codes		 = $this->getService('Table\CodeActiviteTable')
		->getAll();
	$list		 = $this->getService('Table\PlanningTemplateTable')
		->getAll();

	// parcours de la liste des employés
	$employes = array();
	foreach ($empl as $e) {
	    $employes[$e->matricule] = $e;
	    //var_dump($e);
	}

	return array(
	    'templates'	 => $templates,
	    'template'	 => $template,
	    'employes'	 => $employes,
	    'codes'		 => $codes,
	    'typeList'	 => $list,
	    'nohead'	 => $nohead,
	    'fullscreen'	 => $fullscreen,
	    'adapter'	 => $this->getService('Table\PlanningTemplateTable')->getAdapter(),
	);
    }

    public function savetolistAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Données en paramètres
	$post = $this->getRequest()->getPost();

	// Tables
	$planningListTable = $this->getService('Table\PlanningTemplateTable');

	// Nouveau
	$plList			 = new \RHX\Model\Object\PlanningTemplate($planningListTable->getAdapter());
	$plList->id		 = $post->get('list_id');
	$plList->template	 = $post->get('pl_num');
	$plList->site_id	 = $post->get('site_id');
	$plList->edition	 = $post->get('pl_edition');
	$plList->creation	 = $post->get('pl_creation');
	$plList->libelle	 = $post->get('pl_libelle');

	// Sauvegarde
	$planningListTable->save($plList);

	return array(
	    'post'	 => $post,
	    'new'	 => $plList,
	);
    }

    public function addAction() {
	$this->layout('layout/ajax');
	return array();
    }

    public function saveAction() {
	//Changement de template
	$this->layout('layout/ajax');

	// Data POST
	$post = $this->getRequest()
		->getPost();

	// Variables
	$id	 = $post->get('id');
	$prefix	 = $post->get('prefix');
	$code	 = $post->get('code');
	$entree	 = $post->get('entree');
	$sortie	 = $post->get('sortie');

	// Tables
	$EmployeTemplateTable = $this->getService('Table\EmployeTemplateTable');

	// Planning Type de l'employé
	$planningType = $EmployeTemplateTable->get($id);

	// Assignation
	switch ($prefix) {
	    case 'lundi':
		$planningType->lundi_code	 = $code;
		$planningType->lundi_entree	 = $entree;
		$planningType->lundi_sortie	 = $sortie;
		break;

	    case 'mardi':
		$planningType->mardi_code	 = $code;
		$planningType->mardi_entree	 = $entree;
		$planningType->mardi_sortie	 = $sortie;
		break;

	    case 'mercredi':
		$planningType->mercredi_code	 = $code;
		$planningType->mercredi_entree	 = $entree;
		$planningType->mercredi_sortie	 = $sortie;
		break;

	    case 'jeudi':
		$planningType->jeudi_code	 = $code;
		$planningType->jeudi_entree	 = $entree;
		$planningType->jeudi_sortie	 = $sortie;
		break;

	    case 'vendredi':
		$planningType->vendredi_code	 = $code;
		$planningType->vendredi_entree	 = $entree;
		$planningType->vendredi_sortie	 = $sortie;
		break;

	    case 'samedi':
		$planningType->samedi_code	 = $code;
		$planningType->samedi_entree	 = $entree;
		$planningType->samedi_sortie	 = $sortie;
		break;

	    case 'dimanche':
		$planningType->dimanche_code	 = $code;
		$planningType->dimanche_entree	 = $entree;
		$planningType->dimanche_sortie	 = $sortie;
		break;
	}

	// Sauvegarde
	$EmployeTemplateTable->save($planningType);

	return array(
	    'post'		 => $post,
	    'planningType'	 => $planningType,
	);
    }

    public function eraseAction() {
	$this->layout('layout/ajax');
	$num = $this->params()->fromRoute('id', 0);
	$this->getService('RH/Model/Table/PlanningTemplateTable')
		->eraseTemplates($num);
	$this->getService('RH/Model/Table/TemplateListTable')
		->eraseTemplate($num);

	return array();

//	return $this->redirect()
//			->toRoute('RH/default', array('controller'	 => 'template', 'action'	 => 'index'));
    }

    public function templateAction() {
	$this->layout('layout/ajax');
	$post		 = $this->getRequest()->getPost();
	$prefix		 = $post->get('prefix');
	$template_id	 = $post->get('pk');
	$entree		 = '';
	$sortie		 = '';
	$code		 = '';
	$template	 = $this->getService('Table\PlanningTemplateTable')
		->get($template_id);
	$codes		 = $this->getService('Table\CodeActiviteTable')
		->getAll();
	switch ($prefix) {
	    case 'lundi':
		$code	 = $template->lundi;
		$entree	 = $template->lundi_entree;
		$sortie	 = $template->lundi_sortie;
		break;
	    case 'mardi':
		$code	 = $template->mardi;
		$entree	 = $template->mardi_entree;
		$sortie	 = $template->mardi_sortie;
		break;
	    case 'mercredi':
		$code	 = $template->mercredi;
		$entree	 = $template->mercredi_entree;
		$sortie	 = $template->mercredi_sortie;
		break;
	    case 'jeudi':
		$code	 = $template->jeudi;
		$entree	 = $template->jeudi_entree;
		$sortie	 = $template->jeudi_sortie;
		break;
	    case 'vendredi':
		$code	 = $template->vendredi;
		$entree	 = $template->vendredi_entree;
		$sortie	 = $template->vendredi_sortie;
		break;
	    case 'samedi':
		$code	 = $template->samedi;
		$entree	 = $template->samedi_entree;
		$sortie	 = $template->samedi_sortie;
		break;
	    case 'dimanche':
		$code	 = $template->dimanche;
		$entree	 = $template->dimanche_entree;
		$sortie	 = $template->dimanche_sortie;
		break;
	    default:
		$code	 = 0;
		$entree	 = '00:00';
		$sortie	 = '00:00';
		break;
	}

	return array(
	    'post'		 => $post,
	    'code'		 => $code,
	    'entree'	 => $entree,
	    'sortie'	 => $sortie,
	    'template'	 => $template,
	    'codes'		 => $codes,
	    'prefix'	 => $prefix,
	);
    }

    public function savetemplateAction() {
	$this->layout('layout/ajax');

	$post		 = $this->getRequest()
		->getPost();
	$code_activite	 = $post->get('code');
	$entree_heure	 = $post->get('entree_heure');
	$entree_minute	 = $post->get('entree_minute');
	$entree		 = $entree_heure . ':' . $entree_minute;
	$sortie_heure	 = $post->get('sortie_heure');
	$sortie_minute	 = $post->get('sortie_minute');
	$sortie		 = $sortie_heure . ':' . $sortie_minute;
	$prefix		 = $post->get('prefix');
	$template_id	 = $post->get('template_id');

	// Récupérer le template en question
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');
	$template		 = $planningTemplateTable->get($template_id);

//	return array(
//	  'template'	 => $template,
//	  'post'		 => $post,
//	  );

	switch ($prefix) {
	    case 'lundi':
		$template->lundi		 = $code_activite;
		$template->lundi_entree		 = $entree;
		$template->lundi_sortie		 = $sortie;
		break;
	    case 'mardi':
		$template->mardi		 = $code_activite;
		$template->mardi_entree		 = $entree;
		$template->mardi_sortie		 = $sortie;
		break;
	    case 'mercredi':
		$template->mercredi		 = $code_activite;
		$template->mercredi_entree	 = $entree;
		$template->mercredi_sortie	 = $sortie;
		break;
	    case 'jeudi':
		$template->jeudi		 = $code_activite;
		$template->jeudi_entree		 = $entree;
		$template->jeudi_sortie		 = $sortie;
		break;
	    case 'vendredi':
		$template->vendredi		 = $code_activite;
		$template->vendredi_entree	 = $entree;
		$template->vendredi_sortie	 = $sortie;
		break;
	    case 'samedi':
		$template->samedi		 = $code_activite;
		$template->samedi_entree	 = $entree;
		$template->samedi_sortie	 = $sortie;
		break;
	    case 'dimanche':
		$template->dimanche		 = $code_activite;
		$template->dimanche_entree	 = $entree;
		$template->dimanche_sortie	 = $sortie;
		break;
	}
	// Sauvegarder dans la base
	$template->date_edition	 = date('Y-m-d');
	$id			 = $planningTemplateTable->save($template);

	return array(
	    'id'		 => $id,
	    'template'	 => $template,
	);
    }

    public function addyesnoAction() {
	$this->layout('layout/ajax');

	$planning	 = $this->getService('Table\PlanningTemplateTable')
		->getAll();
	$sites		 = $this->getService('Table\SiteTable')
		->getAll();

	return array(
	    'planning'	 => $planning,
	    'sites'		 => $sites,
	);
    }

    public function addemptyAction() {
	$this->layout('layout/ajax');

	// Post
	$post		 = $this->getRequest()->getPost();
	$site_id	 = $post->get('site_id');
	$employes	 = $this->getService('Table\EmployeTable')
		->getEmployesBySite($site_id);
	$site		 = $this->getService('Table\SiteTable')
		->get($site_id);
	$codes		 = $this->getService('Table\CodeActiviteTable')
		->getAll();

	return array(
	    'site'		 => $site,
	    'employes'	 => $employes,
	    'codes'		 => $codes,
	);
    }

    public function templateemptyAction() {
	// Changement de Layout
	$this->layout('layout/ajax');

	// Données en POST
	$post = $this->getRequest()
		->getPost();

	// Liste des Codes d'activités
	$codes = $this->getService('Table\CodeActiviteTable')
		->getAll();

	return array(
	    'codes'	 => $codes,
	    'pk'	 => $post->get('pk'),
	    'prefix' => $post->get('prefix'),
	    'id'	 => $post->get('id'),
	);
    }

    public function savetemplateemptyAction() {
	$this->layout('layout/ajax');

	// Data
	$post		 = $this->getRequest()->getPost();
	$employes	 = $post->get('employe');

	// Table
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');

	// Max
	$max = $planningTemplateTable->getNextTemplateNumber();

	// Boucle
	foreach ($employes as $matricule) {
	    $template = new \RHX\Model\Object\EmployeTemplate();

	    $template->template	 = $max;
	    $template->matricule	 = $matricule;
	    $template->date_edition	 = date('Y-m-d');

	    $template->lundi_code	 = $post->get('lundi-' . $matricule);
	    $template->lundi_entree	 = $post->get('lundi-' . $matricule . '_entree');
	    $template->lundi_sortie	 = $post->get('lundi-' . $matricule . '_sortie');

	    $template->mardi_code	 = $post->get('mardi-' . $matricule);
	    $template->mardi_entree	 = $post->get('mardi-' . $matricule . '_entree');
	    $template->mardi_sortie	 = $post->get('mardi-' . $matricule . '_sortie');

	    $template->mercredi_code	 = $post->get('mercredi-' . $matricule);
	    $template->mercredi_entree	 = $post->get('mercredi-' . $matricule . '_entree');
	    $template->mercredi_sortie	 = $post->get('mercredi-' . $matricule . '_sortie');

	    $template->jeudi_code	 = $post->get('jeudi-' . $matricule);
	    $template->jeudi_entree	 = $post->get('jeudi-' . $matricule . '_entree');
	    $template->jeudi_sortie	 = $post->get('jeudi-' . $matricule . '_sortie');

	    $template->vendredi_code	 = $post->get('vendredi-' . $matricule);
	    $template->vendredi_entree	 = $post->get('vendredi-' . $matricule . '_entree');
	    $template->vendredi_sortie	 = $post->get('vendredi-' . $matricule . '_sortie');

	    $template->samedi_code	 = $post->get('samedi-' . $matricule);
	    $template->samedi_entree = $post->get('samedi-' . $matricule . '_entree');
	    $template->samedi_sortie = $post->get('samedi-' . $matricule . '_sortie');

	    $template->dimanche_code	 = $post->get('dimanche-' . $matricule);
	    $template->dimanche_entree	 = $post->get('dimanche-' . $matricule . '_entree');
	    $template->dimanche_sortie	 = $post->get('dimanche-' . $matricule . '_sortie');

//	    var_dump($template);
	    $id = $employeTemplateTable->save($template);
	    //var_dump($id);
	}
	// Liste des Planning Type
	$templateList		 = new \RHX\Model\Object\PlanningTemplate($planningTemplateTable->getAdapter());
	$templateList->template	 = $max;
	$templateList->creation	 = date('Y-m-d');
	$templateList->edition	 = date('Y-m-d');
	$templateList->site_id	 = $post->get('site');
	$templateList->libelle	 = $post->get('libelle');
	$id			 = $planningTemplateTable->save($templateList);
	//var_dump($templateList);

	return $this->redirect()
			->toRoute('RHX/default', array('controller'	 => 'template', 'action'	 => 'index'));

//	return array(
//	    'post'		 => $post,
//	    'employes'	 => $employes,
//	);
    }

    public function addotherAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Données en Paramètre
	$post = $this->getRequest()->getPost();

	// Tables
	$employeTable		 = $this->getService('Table\EmployeTable');
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');
	$codeActiviteTable	 = $this->getService('Table\CodeActiviteTable');
	$siteTable		 = $this->getService('Table\SiteTable');

	// Liste des employés
	$employeList = $employeTable->getAllBySite($post->get('site_id'));

	// Planning à Copier
	$planningTemplateList = $planningTemplateTable->getTemplates($post->get('pl_num'));

	// Liste des codes d'activités
	$codeList = $codeActiviteTable->getAll();

	// Site
	$site = $siteTable->get($post->get('site_id'));

	return array(
	    'post'		 => $post,
	    'employes'	 => $employeList,
	    'templates'	 => $planningTemplateList,
	    'codes'		 => $codeList,
	    'site'		 => $site,
	);
    }

    public function adddispoAction() {
	$this->layout('layout/ajax');

	// Post
	$post		 = $this->getRequest()->getPost();
	$site_id	 = $post->get('site_id');
	$employes	 = $this->getService('Table\EmployeTable')
		->getEmployesBySite($site_id);
	$site		 = $this->getService('Table\SiteTable')
		->get($site_id);
	$codes		 = $this->getService('Table\CodeActiviteTable')
		->getAll();
	$dispoTable	 = $this->getService('Table\EmployeDisponibiliteTable');
	$disponibilites	 = array();
	$emplist	 = array();
	// Parcours des disponibilités des employés
	foreach ($employes as $emp) {
	    $emplist[]				 = $emp;
	    $dispo					 = $dispoTable->getDispoByEmploye($emp->matricule);
	    $disponibilites[(int) $emp->matricule]	 = $dispo;
	}

	return array(
	    'site'		 => $site,
	    'employes'	 => $emplist,
	    'codes'		 => $codes,
	    'dispo'		 => $disponibilites,
	);

	return array();
    }

    public function loadtemplateAction() {
	$this->layout('layout/ajax');
	$post		 = $this->getRequest()->getPost();
	$site_id	 = $post->get('site');
	$num		 = $post->get('num');
	$template	 = $this->getService('Table\TemplateListTable')
		->getTemplatesBySite($site_id, $num);
	$empl		 = $this->getService('Table\EmployeTable')
		->getAllBySite($site_id);
	$codes		 = $this->getService('Table\CodeActiviteTable')
		->getAll();
	$list		 = $this->getService('Table\TemplateListTable')
		->getAll();
	$employes	 = array();
	foreach ($empl as $e) {
	    $employes[$e->matricule] = $e;
	}

	return array(
	    'template'	 => $template,
	    'employes'	 => $employes,
	    'codes'		 => $codes,
	    'typeList'	 => $list,
	);
    }

    public function templatetypelistAction() {
	$this->layout('layout/ajax');

	$id	 = (int) $this->params()->fromRoute('id', 0);
	$tplList = $this->getService('Table\PlanningTemplateTable')
		->getAll();
	$list	 = array();
	foreach ($tplList as $tpl) {
	    if ($id == (int) $tpl->site_id) {
		$list[] = $tpl;
	    }
	}

	return array(
	    'list' => $list,
	);
    }

    public function popinAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Post
	$post = $this->getRequest()
		->getPost();

	// Variables
	$id		 = $post->get('id');
	$prefix		 = $post->get('prefix');
	$matricule	 = $post->get('matricule');

	// Tables
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$codeTable		 = $this->getService('Table\CodeActiviteTable');

	// Template
	$template = $employeTemplateTable->getOne($id, $matricule);

	// Valeurs à Afficher
	$code	 = 3;
	$entree	 = '00:00';
	$sortie	 = '00:00';

	// Prefix
	switch ($prefix) {
	    case 'lundi':
		$code	 = $template->lundi_code;
		$entree	 = $template->lundi_entree;
		$sortie	 = $template->lundi_sortie;
		break;
	    case 'mardi':
		$code	 = $template->mardi_code;
		$entree	 = $template->mardi_entree;
		$sortie	 = $template->mardi_sortie;
		break;
	    case 'mercredi':
		$code	 = $template->mercredi_code;
		$entree	 = $template->mercredi_entree;
		$sortie	 = $template->mercredi_sortie;
		break;
	    case 'jeudi':
		$code	 = $template->jeudi_code;
		$entree	 = $template->jeudi_entree;
		$sortie	 = $template->jeudi_sortie;
		break;
	    case 'vendredi':
		$code	 = $template->vendredi_code;
		$entree	 = $template->vendredi_entree;
		$sortie	 = $template->vendredi_sortie;
		break;
	    case 'samedi':
		$code	 = $template->samedi_code;
		$entree	 = $template->samedi_entree;
		$sortie	 = $template->samedi_sortie;
		break;
	    case 'dimanche':
		$code	 = $template->dimanche_code;
		$entree	 = $template->dimanche_entree;
		$sortie	 = $template->dimanche_sortie;
		break;
	}

	$codeList	 = $codeTable->getAll();
	$codes		 = array();
	// Parcours des codes d'activités
	foreach ($codeList as $c) {
	    $codes[$c->id] = $c;
	}

	return array(
	    'template'	 => $template,
	    'code'		 => $code,
	    'entree'	 => $entree,
	    'sortie'	 => $sortie,
	    'codes'		 => $codes,
	    'prefix'	 => $prefix,
	);
    }

    public function popinNewAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Post
	$post = $this->getRequest()
		->getPost();

	// Variables
	$id		 = $post->get('id');
	$prefix		 = $post->get('prefix');
	$matricule	 = $post->get('matricule');
	$pk		 = $post->get('pk');

	// Tables
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$codeTable		 = $this->getService('Table\CodeActiviteTable');

	// Valeurs à Afficher
	$code	 = 3;
	$entree	 = '00:00';
	$sortie	 = '00:00';


	$codeList	 = $codeTable->getAll();
	$codes		 = array();
	// Parcours des codes d'activités
	foreach ($codeList as $c) {
	    $codes[$c->id] = $c;
	}

	return array(
	    'code'		 => $code,
	    'entree'	 => $entree,
	    'sortie'	 => $sortie,
	    'codes'		 => $codes,
	    'prefix'	 => $prefix,
	    'matricule'	 => $matricule,
	    'pk'		 => $pk,
	);
    }

}

?>
